package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WdyTxnHistory;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.service.IWdyTxnHistoryService;
import com.ruoyi.system.service.IWdyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 交易流水 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-03-12
 */
@Controller
@RequestMapping("/system/wdyTxnHistory")
public class WdyTxnHistoryController extends BaseController
{
    private String prefix = "system/wdyTxnHistory";
	
	@Autowired
	private IWdyTxnHistoryService wdyTxnHistoryService;
	@Autowired
	private IWdyUserService wdyUserService;
	@RequiresPermissions("system:wdyTxnHistory:view")
	@GetMapping()
	public String wdyTxnHistory()
	{
	    return prefix + "/wdyTxnHistory";
	}
	
	/**
	 * 查询交易流水列表
	 */
	@RequiresPermissions("system:wdyTxnHistory:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WdyTxnHistory wdyTxnHistory)
	{
		startPage();
        List<WdyTxnHistory> list = wdyTxnHistoryService.selectWdyTxnHistoryList(wdyTxnHistory);
		for(WdyTxnHistory e : list) {
			QueryWrapper<WdyUser> query = new QueryWrapper<>();
			query.eq("user_id", e.getUserId());
			WdyUser user = wdyUserService.getOne(query);
			if(user != null) {
				e.addExtraData("mobile", user.getUserName());
			}
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出交易流水列表
	 */
	@RequiresPermissions("system:wdyTxnHistory:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WdyTxnHistory wdyTxnHistory)
    {
    	List<WdyTxnHistory> list = wdyTxnHistoryService.selectWdyTxnHistoryList(wdyTxnHistory);
        ExcelUtil<WdyTxnHistory> util = new ExcelUtil<WdyTxnHistory>(WdyTxnHistory.class);
        return util.exportExcel(list, "wdyTxnHistory");
    }
	
	/**
	 * 新增交易流水
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存交易流水
	 */
	@RequiresPermissions("system:wdyTxnHistory:add")
	@Log(title = "交易流水", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WdyTxnHistory wdyTxnHistory)
	{		
		return toAjax(wdyTxnHistoryService.insertWdyTxnHistory(wdyTxnHistory));
	}

	/**
	 * 修改交易流水
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyTxnHistory wdyTxnHistory = wdyTxnHistoryService.selectWdyTxnHistoryById(id);
		mmap.put("wdyTxnHistory", wdyTxnHistory);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存交易流水
	 */
	@RequiresPermissions("system:wdyTxnHistory:edit")
	@Log(title = "交易流水", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WdyTxnHistory wdyTxnHistory)
	{		
		return toAjax(wdyTxnHistoryService.updateWdyTxnHistory(wdyTxnHistory));
	}
	
	/**
	 * 删除交易流水
	 */
	@RequiresPermissions("system:wdyTxnHistory:remove")
	@Log(title = "交易流水", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wdyTxnHistoryService.deleteWdyTxnHistoryByIds(ids));
	}
	
}
